package com.AdvancedDemos.LearnVariousAPI.时间日期类.传统时间日期类;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class LearnCalendar {
    public static void main(String[] args) {
        /**
         * Calendar类代表的是系统此刻时间对应的日历
         * 通过它可以单独获取,修改时间中的年,月,日,时,分,秒等
         *
         * 它是一个抽象类,可以通过Calendar rightNow = Calendar.getInstance();获得其直接子类GregorianCalendar的实例
         */

        /*
        public static Calendar getInstance() --- 获得当前日历对象
        public int get(int field) --- 获取日历中的某个信息
        public final Date getTime() --- 获取日期对象
        public long getTimeInMillis() --- 获取时间毫秒值
        public void set(int field, int value) --- 修改日历的某个信息
        public void add(int field,int amout) --- 为某个信息增加/减少特定的值
         */

        Calendar instance = Calendar.getInstance();
        System.out.println(instance);
        System.out.println("-------------");

        System.out.println("今年是" + instance.get(Calendar.YEAR) + "年");
        System.out.println("本月是" + (instance.get(Calendar.MONTH) + 1) + "月");//月份有偏移,从0到11月
        System.out.println("今天是今年第" + instance.get(Calendar.DAY_OF_YEAR) + "天");
        System.out.println("今天是本月第" + instance.get(Calendar.DAY_OF_MONTH) + "天");
        System.out.println("今天是本周第" + instance.get(Calendar.DAY_OF_WEEK) + "天");

        System.out.println("-------------");
        Date date = instance.getTime();//转成Date对象,便于打印
        System.out.println(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
        String format = simpleDateFormat.format(date);
        System.out.println(format);


        System.out.println("-------------");
        long timeInMillis = instance.getTimeInMillis();//获得毫秒值
        System.out.println(timeInMillis);
//        System.out.println(new Date(timeInMillis));
        System.out.println(date.getTime());

        System.out.println("-------------");
        instance.set(2500, 11, 12, 12, 12, 12);//月份0-11
        date = instance.getTime();
        System.out.println(simpleDateFormat.format(date));
        instance.set(Calendar.YEAR, 1998);
        instance.set(Calendar.MONTH, 2);
        instance.set(Calendar.DATE, 25);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
//        instance.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
        System.out.println(simpleDateFormat.format(instance.getTime()));

        System.out.println("-------------");
        instance = Calendar.getInstance();
        System.out.println(simpleDateFormat.format(instance.getTime()));
        System.out.println("===");
        instance.add(Calendar.YEAR, 1);//增加
        System.out.println(simpleDateFormat.format(instance.getTime()));
        System.out.println("===");
        instance.add(Calendar.DAY_OF_WEEK, 100);//增加
        System.out.println("今天是今年第" + instance.get(Calendar.DAY_OF_YEAR) + "天");
        System.out.println("===");
        System.out.println(simpleDateFormat.format(instance.getTime()));
        System.out.println("===");
        instance.add(Calendar.HOUR_OF_DAY, -1);//减少
        instance.add(Calendar.DAY_OF_MONTH, -10);//减少
        System.out.println(simpleDateFormat.format(instance.getTime()));
        System.out.println("===");


        System.out.println("-------------");
        //Calendar也支持设置时区
        /*public static Calendar getInstance(TimeZone zone)*/
        /*public static synchronized TimeZone getTimeZone(String ID) {
                return getTimeZone(ID, true);
         }*/
        System.out.println("America/New_York时间 : " + simpleDateFormat.format(Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of("America/New_York"))).getTime()));

    }
}
